package yesjava.news.web;

import com.alibaba.fastjson.JSON;
import yesjava.news.service.NewsService;
import yesjava.news.service.NewsServiceImpl;
import yesjava.news.utils.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@WebServlet("/del-news.do")
public class DelNewsServlet extends HttpServlet {
    private NewsService newsService = new NewsServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String[] idsStr = request.getParameterValues("id");
        ArrayList<String> list = new ArrayList<>(List.of(idsStr));
        Integer[] ids = list.stream().map(e -> Integer.parseInt(e)).collect(Collectors.toList()).toArray(new Integer[]{});
        //Integer[] ids = idList.stream().toArray(Integer::new);
       /* String idTxt = request.getParameter("id");
//        System.out.println("新闻的id:"+id);*/
        newsService.removeById(ids);
        Map<String,Object> map  = new HashMap<>();
        map.put("success",true);
        map.put("code",200);
        map.put("msg","哈哈，竟然删除了！");
        JSONUtil.printJSON(map,response);


    }
}
